package com.itheima.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //分页查询
        Page<Permission> page = permissionDao.findPageByQueryString(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public Permission findById(Integer id) {

        return permissionDao.findById(id);
    }

    @Override
    public void delById(Integer id) {
        //1.查询权限是否和角色存在关联
        Long count = permissionDao.findCountById(id);

        if (count > 0){
            throw new RuntimeException("权限和角色有关联不能删除！！！！！！！");
        }else{
            //2.如果没有关联之间删除
            permissionDao.delById(id);
        }
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
}
